opendir(DH,"/home/uec-00/yapingli/code/Allele_specific_methylation/") or die;
foreach my $file(readdir(DH)){
	if($file=~/methylCGsRich_ASM_(\w+)Merge_(\w+)_pValue.txt/){
		my $sample=$1;
		my $chr=$2;
		open(FH,"</home/uec-00/yapingli/code/Allele_specific_methylation/$file");
		my @in=<FH>;
		chomp(@in);
		close(FH);
		my @logp_out;
		my @methy_out;
		my @unmethy_out;
		my $logp_title="track type=bedGraph name=\"".$sample." ".$chr." -log2 P value\" description=\"".$sample." ".$chr." -log2 P value\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
		my $methy_title="track type=bedGraph name=\"".$sample." ".$chr." log2 methylation fraction_to_expectation\" description=\"".$sample." ".$chr." log2 methylation fraction_to_expectation\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
		my $unmethy_title="track type=bedGraph name=\"".$sample." ".$chr." log2 unmethylation fraction_to_expectation\" description=\"".$sample." ".$chr." log2 unmethylation fraction_to_expectation\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
		
		push(@logp_out,"$logp_title\n");
		push(@methy_out,"$methy_title\n");
		push(@unmethy_out,"$unmethy_title\n");
		foreach my $line(@in){
			my @splitin = split "\t",$line;
			push(@logp_out,"$chr\t$splitin[1]\t$splitin[1]\t$splitin[3]\n");
			push(@methy_out,"$chr\t$splitin[1]\t$splitin[1]\t$splitin[4]\n");
			push(@unmethy_out,"$chr\t$splitin[1]\t$splitin[1]\t$splitin[5]\n");
		}
		my $logp_out_file="methylCGsRich_ASM_".$sample."Merge_".$chr."_logP_unjusted.bedGraph";
		my $methy_out_file="methylCGsRich_ASM_".$sample."Merge_".$chr."_log2_methy.bedGraph";
		my $unmethy_out_file="methylCGsRich_ASM_".$sample."Merge_".$chr."_log2_unmethy.bedGraph";
		open(OUT,">/home/uec-00/yapingli/code/Allele_specific_methylation/$logp_out_file");
		print OUT @logp_out;
		close(OUT);
		open(OUT,">/home/uec-00/yapingli/code/Allele_specific_methylation/$methy_out_file");
		print OUT @methy_out;
		close(OUT);
		open(OUT,">/home/uec-00/yapingli/code/Allele_specific_methylation/$unmethy_out_file");
		print OUT @unmethy_out;
		close(OUT);
		
	}
	if($file=~/methylCGsRich_ASM_(\w+)Merge_(\w+)_SNP_afterBaseQfilter.txt/){
		my $sample=$1;
		my $chr=$2;
		open(FH,"</home/uec-00/yapingli/code/Allele_specific_methylation/$file");
		my @in=<FH>;
		chomp(@in);
		close(FH);
		my @snp_out;
		my $snp_title="track type=bedGraph name=\"".$sample." ".$chr." SNP\" description=\"".$sample." ".$chr." SNP\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
		push(@snp_out,"$snp_title\n");
		foreach my $line(@in){
			my @splitin = split "\t",$line;
			my $ratio=$splitin[2]/$splitin[1];
			push(@snp_out,"$chr\t$splitin[0]\t$splitin[0]\t$ratio\n");
		}
		my $snp_out_file="methylCGsRich_ASM_".$sample."Merge_".$chr."_SNP.bedGraph";
		open(OUT,">/home/uec-00/yapingli/code/Allele_specific_methylation/$snp_out_file");
		print OUT @snp_out;
		close(OUT);
	}
}